﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace WebLibraryMaker
{
    public static class AspnetCompiler
    {
        public static int PrecompileWebApplication(Pathes pathes, bool debug)
        {
            string debugOption = "";
            if (debug) debugOption = "-d ";

            Process process = new Process();
            process.StartInfo.FileName = Path.Combine(pathes.DotNetDirectory, "aspnet_compiler.exe");
            process.StartInfo.Arguments = String.Concat(debugOption, "-f \"", pathes.TemporaryDirectory, "\" -v \"", pathes.ProjectName, "\" -p \"", pathes.ProjectDirectory, "\"");
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.UseShellExecute = false;
            process.Start();
            Console.Write(process.StandardOutput.ReadToEnd());
            Console.Error.Write(process.StandardError.ReadToEnd());
            process.WaitForExit();
            if (process.ExitCode != 0) return process.ExitCode;

            Directory.SetCurrentDirectory(pathes.CompiledDirectory);

            return 0;
        }
    }
}
